package com.sun.jdktimer;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

public class Timer01 {
    public static void main(String[] args) {
        Timer t1 = new Timer();
        for (int i = 0; i < 2; i++) {
            /**
             * schedule 定义两秒执行一次，但是前一个任务没有执行结束会导致下一个任务延期执行
             * scheduleAtFixedRate 完全按照规定时间执行 隔三秒后将之前三秒钟任务补齐
             */
//            t1.schedule(new TimerTask01(i + ""),new Date(),2000);
            t1.scheduleAtFixedRate(new TimerTask01(i + ""),new Date(),2000);
        }
    }
}
class TimerTask01 extends TimerTask{

    private String name;

    public TimerTask01(String name) {
        this.name = name;
    }

    @Override
    public void run() {
        try {
            System.out.println(name + "：TimerTask01正在执行....." + LocalDateTime.now());
            Thread.sleep(3000);
            System.out.println(LocalDateTime.now());

        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

